package com.example.stuwork.service.impl;

import com.example.stuwork.dao.SystemAnnouncementDao;
import com.example.stuwork.pojo.SystemAnnouncement;
import com.example.stuwork.service.SystemAnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 系统公告服务实现类
 */
@Service
public class SystemAnnouncementServiceImpl implements SystemAnnouncementService {

    @Autowired
    private SystemAnnouncementDao systemAnnouncementDao;
    
    /**
     * 获取所有系统公告
     *
     * @return 系统公告列表
     */
    @Override
    public List<SystemAnnouncement> findAll() {
        return systemAnnouncementDao.findAll();
    }
    
    /**
     * 根据ID获取系统公告
     *
     * @param announcementId 公告ID
     * @return 系统公告
     */
    @Override
    public SystemAnnouncement getById(Long announcementId) {
        if (announcementId == null) {
            return null;
        }
        return systemAnnouncementDao.getById(announcementId);
    }
    
    /**
     * 添加系统公告
     *
     * @param announcement 系统公告信息
     * @return 是否成功
     */
    @Override
    @Transactional
    public boolean addAnnouncement(SystemAnnouncement announcement) {
        if (announcement == null || announcement.getTitle() == null || announcement.getContent() == null) {
            return false;
        }
        
        return systemAnnouncementDao.insert(announcement) > 0;
    }
    
    /**
     * 更新系统公告
     *
     * @param announcement 系统公告信息
     * @return 是否成功
     */
    @Override
    @Transactional
    public boolean updateAnnouncement(SystemAnnouncement announcement) {
        if (announcement == null || announcement.getAnnouncementId() == null) {
            return false;
        }
        
        // 验证公告是否存在
        SystemAnnouncement existingAnnouncement = systemAnnouncementDao.getById(announcement.getAnnouncementId());
        if (existingAnnouncement == null) {
            return false;
        }
        
        // 验证必填字段
        if (announcement.getTitle() == null || announcement.getContent() == null) {
            return false;
        }
        
        return systemAnnouncementDao.update(announcement) > 0;
    }
    
    /**
     * 删除系统公告
     *
     * @param announcementId 公告ID
     * @return 是否成功
     */
    @Override
    @Transactional
    public boolean deleteAnnouncement(Long announcementId) {
        if (announcementId == null) {
            return false;
        }
        
        // 验证公告是否存在
        SystemAnnouncement existingAnnouncement = systemAnnouncementDao.getById(announcementId);
        if (existingAnnouncement == null) {
            return false;
        }
        
        return systemAnnouncementDao.delete(announcementId) > 0;
    }
} 